package cn.lngaosu.lwyd.module.integration.controller.admin.weibouserscounts;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.weibouserscounts.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weibouserscounts.WeiboUserscountsDO;
import cn.lngaosu.lwyd.module.integration.service.weibouserscounts.WeiboUserscountsService;

@Tag(name = "管理后台 - 微博用户基本信息")
@RestController
@RequestMapping("/integration/weibo-userscounts")
@Validated
public class WeiboUserscountsController {

    @Resource
    private WeiboUserscountsService weiboUserscountsService;

    @PostMapping("/create")
    @Operation(summary = "创建微博用户基本信息")
    @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:create')")
    public CommonResult<Long> createWeiboUserscounts(@Valid @RequestBody WeiboUserscountsSaveReqVO createReqVO) {
        return success(weiboUserscountsService.createWeiboUserscounts(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新微博用户基本信息")
    @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:update')")
    public CommonResult<Boolean> updateWeiboUserscounts(@Valid @RequestBody WeiboUserscountsSaveReqVO updateReqVO) {
        weiboUserscountsService.updateWeiboUserscounts(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除微博用户基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:delete')")
    public CommonResult<Boolean> deleteWeiboUserscounts(@RequestParam("id") Long id) {
        weiboUserscountsService.deleteWeiboUserscounts(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微博用户基本信息")
                @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:delete')")
    public CommonResult<Boolean> deleteWeiboUserscountsList(@RequestParam("ids") List<Long> ids) {
        weiboUserscountsService.deleteWeiboUserscountsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微博用户基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:query')")
    public CommonResult<WeiboUserscountsRespVO> getWeiboUserscounts(@RequestParam("id") Long id) {
        WeiboUserscountsDO weiboUserscounts = weiboUserscountsService.getWeiboUserscounts(id);
        return success(BeanUtils.toBean(weiboUserscounts, WeiboUserscountsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微博用户基本信息分页")
    @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:query')")
    public CommonResult<PageResult<WeiboUserscountsRespVO>> getWeiboUserscountsPage(@Valid WeiboUserscountsPageReqVO pageReqVO) {
        PageResult<WeiboUserscountsDO> pageResult = weiboUserscountsService.getWeiboUserscountsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeiboUserscountsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微博用户基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('integration:weibo-userscounts:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeiboUserscountsExcel(@Valid WeiboUserscountsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeiboUserscountsDO> list = weiboUserscountsService.getWeiboUserscountsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微博用户基本信息.xls", "数据", WeiboUserscountsRespVO.class,
                        BeanUtils.toBean(list, WeiboUserscountsRespVO.class));
    }

}